//
//  ESTMineViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/11/2.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTMineViewController.h"
#import "ESTSettingTableViewController.h"
#import "UIButton+WebCache.h"
#import "ESTMineCollectionViewCell.h"
@interface ESTMineViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
/**
 *  UICollectionView
 */
@property(nonatomic, strong) UICollectionView *collectionView;
/**
 *  数据源
 */
@property(nonatomic, strong) NSArray<NSArray<NSString *> *> *dataArray;
@end

@implementation ESTMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView];
    [self setupHeaderView];
    [self setupCollectionView];
}
#pragma mark - 设置UI
- (void)setupTitleView {
    TYZView *titleView = [[TYZView alloc] init];
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(DE_NavigationHeight);
    }];
    titleView.backgroundColor = ThemeColor;
    TYZButton *leftButton = [[TYZButton alloc] init];
    [titleView addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView).offset(10);
        make.left.equalTo(titleView).offset(20);
    }];
    [leftButton setImage:[UIImage imageNamed:@"message_redPoint"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(didClickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    
    TYZButton *rightButton = [[TYZButton alloc] init];
    [titleView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleView).offset(10);
        make.right.equalTo(titleView).offset(-20);
    }];
    [rightButton setImage:[UIImage imageNamed:@"manager_mine_set_icon"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setupHeaderView {
    TYZView *headerView = [[TYZView alloc] init];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(DE_NavigationHeight);
        make.height.mas_equalTo(200);
    }];
    TYZImageView *imageView = [[TYZImageView alloc] initWithImage:[UIImage imageNamed:@"manager_headerbg"]];
    [headerView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(headerView);
    }];
    
    TYZButton *imageButton = [[TYZButton alloc] init];
    [headerView addSubview:imageButton];
    [imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:UserInfo.EMP_INFO.firstObject.HEAD_PORTRAIT_URL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placeholder_person"]];
    [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headerView).offset(-20);
        make.centerX.equalTo(headerView);
        make.width.height.mas_equalTo(80);
    }];
    imageButton.layer.cornerRadius = 40;
    imageButton.layer.masksToBounds = YES;
    [imageButton addTarget:self action:@selector(didClickHeaderImageButton) forControlEvents:UIControlEventTouchUpInside];
    
    TYZLabel *nameLabel = [[TYZLabel alloc] init];
    nameLabel.text = [NSString stringWithFormat:@"%@（%@）", UserInfo.EMP_INFO.firstObject.EMPLOYEE_NAME, UserInfo.EMP_INFO.firstObject.SYSTEM_STATION_NAME];
    [headerView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageButton);
        make.top.equalTo(imageButton.mas_bottom).offset(10);
    }];
    nameLabel.textColor = TYZWhiteColor;
    nameLabel.font = TYZFont(CoreInfo.fontSize);
    
    TYZLabel *detailLabel = [[TYZLabel alloc] init];
    detailLabel.text = UserInfo.EMP_INFO.firstObject.PHONE_NUMBER;
    [headerView addSubview:detailLabel];
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(10);
    }];
    detailLabel.textColor = TYZWhiteColor;
    detailLabel.font = TYZFont(CoreInfo.fontSize * 0.8);
}
- (void)setupCollectionView {
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(DE_NavigationHeight + 200);
    }];
}
#pragma mark - actions
- (void)didClickLeftButton {
    
}
- (void)didClickRightButton {
    [self.navigationController pushViewController:[[ESTSettingTableViewController alloc] init] animated:YES];
}
- (void)didClickHeaderImageButton {
    
}
#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kWindowWidth - 4) / 3, 120);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ESTMineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ESTMineCollectionViewCell" forIndexPath:indexPath];
    NSArray *arr = self.dataArray[indexPath.item];
    cell.iconView.image = [UIImage imageNamed:arr.firstObject];
    cell.titleLabel.text = arr.lastObject;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}
- (NSArray<NSArray<NSString *> *> *)dataArray {
    if (_dataArray == nil) {
        _dataArray = @[@[@"mine_car_store", [@"车辆库存" S]],
                       @[@"mine_car_data", [@"车型资料" S]],
                       @[@"mine_knowledge_base", [@"知识库" S]],
                       @[@"mine_target_set", [@"指标设置" S]],
                       @[@"mine_help_center", [@"帮助中心" S]],
                       @[@"mine_suggest_feedback", [@"意见反馈" S]],
                       @[@"mine_work_space", [@"工作圈" S]],
                       @[@"mine_user_manager", [@"用户管理" S]],
                       @[]];
    }
    return _dataArray;
}
#pragma mark - setter & getter
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // 同一行相邻两个cell的最小间距
        layout.minimumInteritemSpacing = 2;
        // 同一列相邻两个cell的最小间距
        layout.minimumLineSpacing = 2;
        // 竖向滑动
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[TYZCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = ESTBackgroundColor;
        [_collectionView registerNib:[UINib nibWithNibName:@"ESTMineCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ESTMineCollectionViewCell"];
    }
    return _collectionView;
}
@end
